
# First check for the presence of a real terminal.
if [ ! -t 0 ]; then
	# STDIN is not a terminal, so don't wait for any user input.
	# This is the case when you use an SCP client such as WinSCP 4.0 .
	# In this case, no need to launch tmux. 
	# What's more, if you go on, the Bash script will later freeze on 
	#
	#	read -n 1 -t 1 tmpchar
	#
	# Thanks to http://tldp.org/LDP/abs/html/fto.html
	return 0
fi

if [[ $SSH_CONNECTION == *"127.0.0.22 22" ]]; then
	# You can use it with socat TCP redirection:
	#
	#   socat TCP4-LISTEN:2222,fork,nodelay,reuseaddr TCP:127.0.0.22:22
	#
	# If someone(e.g. VSLinux) SSH connects to this Linux
	# via TCP port 2222, then Easytmux will NOT launch.
	return 0
fi

if [ "$DONT_LAUNCH_EASYTMUX" = 1 ]; then
	# Give user a way to suppress Easytmux's launch via env-var
	return 0
fi


# The "default" behavior is to launch tmux on each shell login.
# But give 3 seconds timeout so that user can opt NOT to launch tmux.

function is_tmux_ls_bugfree()
{
	# ``tmux ls`` has bug in version earlier than 1.7 .
	# When some session exists, 
	#	tmux ls > /dev/null
	# goes freeze, and you have to type Ctrl+\ to quit it.
	#
	# return 0 if it does NOT have such bug(means bug-free). 
	# return 1 if it has such bug.
	# -- You know, 0 means positive(true) in Bash's context, non-0 means negative(false).
	
	tmux_V_output=$(tmux -V) 
		# Sample output:
		#	tmux 1.4 
		#
	n=${tmux_V_output#tmux } # strip leading "tmux "
	if [ 1.6 = "$n" ] || [ 1.5 = "$n" ] || [ 1.4 = "$n" ] || [ 1.3 = "$n" ]; then
		return 1
	else
		return 0
	fi
}

function is_tmux_v1()
{
	# Check whether we are running (old) tmux version 1.x .
	# If yes, return 0; if no, return 1 .
	
	tmux_V_output=$(tmux -V) 
		# Sample output:
		#	tmux 1.6
		#	tmux 3.0a
	
	# https://stackoverflow.com/a/2172367/151453
	if [[ "$tmux_V_output" == "tmux 1."* ]]; then
		return 0
	else 
		return 1
	fi
}

function confirm_to_run_tmux()
{
	# This function presents a three-second count-down. When count-down drops to 0,
	# or use hits Enter within this period, it returns 0 to indicate tmux should be run. 
	# In case user press N, it returns 1 to tell tmux should not run.
	
	# Thanks to http://stackoverflow.com/a/5861713/151453 for erase-line trick,
	# so that I can display 3-2-1 count down on the same terminal line.
	local tmpchar
	for pc in 3 2 1; do
		echo -ne "\rWill launch tmux in $pc seconds. Press N to suppress.\033[0K"
		if read -n 1 -t 1 tmpchar; then
			if [ "$tmpchar" = "n" ] || [ "$tmpchar" = "N" ]; then
				echo 
				echo "You pressed $tmpchar, tmux will not be launched."
				return 1
			elif [ "$tmpchar" = $'\x1b' ]; then
				echo
				echo "You pressed ESC, tmux will not be launched."
				return 1
			elif [ "$tmpchar" = "$(echo -e '')" ]; then
				# User press Enter directly, ok to launch_tmux
				# Note: The $'\x0a' trick seems not to work here.
				return 0
			fi
		fi
	done
	return 0 # ok to launch tmux
}


tmp_tmux_banner_fname="/tmp/tmux.banner.$USER"
	# This file is generated by launch_tmux.py

if [ "$TMUX" = "" ]; then
	# This is a brand new shell login, so we launch tmux,
	# but give a 3-second "user confirm" timeout.

	if is_tmux_ls_bugfree; then
		tmux ls > /dev/null 2>&1
	else
		false # so, let three-second delay always executed
	fi
	
	# 3-second wait
	if [ $? != 0 ]; then
		# ``tmux ls`` return error, which means current user does not have any existing tmux session;
		# in this situation, we present three-second countdown.
		#
		# ``tmux ls`` typical "error" message (tmux 1.8.5, Ubuntu 14.04):
		#	failed to connect to server
		#
		if ! confirm_to_run_tmux; then
			return 0
		fi
	fi

	if ! which tmux > /dev/null ; then
		echo "Software package \"tmux\" not installed, I cannot launch tmux."
		return 1
	fi
	
	if is_tmux_v1; then
		# Use old tmux.conf syntax
		tmux_conf_filename=tmux-v1.conf
	else
		# Use new tmux.conf syntax
		tmux_conf_filename=tmux.conf
	fi
	
	python_exe=$(which python)
	if [ -z "$python_exe" ]; then
		python_exe=python3
	fi
	
	tmuxcmd="$("$python_exe" "$dir_qlbox/launch_tmux.py" --tmuxconf="$dir_qlbox/$tmux_conf_filename")"
		# Grab the .py's stdout content as to-execute shell command.
		# The double-quotes surrounding $(...) is optional, I put them here 
		# just to demonstrate nested double-quotes inside $(...) should not
		# be \ escaped.

#	echo '' && echo "===$tmuxcmd"  && sleep 2 # debug
	if [ $? = 0 ]; then
		if [ -z "$tmuxcmd" ]; then 
			return 0
		else 
			sh -c "$tmuxcmd"
			# This execute real tmux cmd, e.g.
			#	tmux -f .../tmux.conf new -s sessname
			#	tmux attach -t sessname
		fi
	else
		echo
		echo "Info: tmux cannot be launched because launch_tmux.py failed!"
		echo
		# Even if launch_tmux.py fails, let user stay in Bash.
	fi
else
	# We are in a tmux session
	# Find the banner file and display it to user.
	if [ -f "$tmp_tmux_banner_fname" ]; then
		cat "$tmp_tmux_banner_fname"
		rm "$tmp_tmux_banner_fname"
	fi
	
	alias srename='tmux rename'
	alias tabrename='tmux rename-window'
	
	# The following two is ok for tmux 2.7 .
	alias tmouseon='tmux set-option -g mouse on'
	alias tmouseoff='tmux set-option -g mouse off'
fi


